package com.zzh.partnersys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzh.partnersys.common.exception.BusinessException;
import com.zzh.partnersys.common.exception.ResultCodeEnum;
import com.zzh.partnersys.common.exception.ResultUtils;
import com.zzh.partnersys.entity.CollabDocumentDO;
import com.zzh.partnersys.entity.request.CollabDocumentCreateRequest;
import com.zzh.partnersys.entity.request.CollabDocumentQueryRequest;
import com.zzh.partnersys.entity.request.CollabDocumentUpdateRequest;
import com.zzh.partnersys.entity.vo.CollabDocumentVO;
import com.zzh.partnersys.entity.vo.CollabDocumentVersionVO;
import com.zzh.partnersys.service.CollabDocumentService;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文档控制�?
 *
 * @author zzh
 * @since 2024-11-01
 */
@RestController
@RequestMapping("/collab/document")
public class CollabDocumentController {

    @Resource
    private CollabDocumentService collabDocumentService;

    /**
     * 创建文档
     *
     * @param request 创建请求
     * @param request1 Http请求
     * @return 创建结果
     */
    @PostMapping("/create")
//    //@AuthCheck(mustRole = "user")
    public ResultUtils<CollabDocumentDO> createDocument(@RequestBody CollabDocumentCreateRequest request, HttpServletRequest request1) {
        Long userId = getCurrentUserId(request1);
        CollabDocumentDO document = collabDocumentService.createDocument(request, userId);
        return ResultUtils.ok(document);
    }

    /**
     * 更新文档
     *
     * @param request 更新请求
     * @param request1 Http请求
     * @return 更新结果
     */
    @PostMapping("/update")
//    //@AuthCheck(mustRole = "user") todo 权限控制
    public ResultUtils<CollabDocumentDO> updateDocument(@RequestBody CollabDocumentUpdateRequest request, HttpServletRequest request1) {
        Long userId = getCurrentUserId(request1);
        CollabDocumentDO document = collabDocumentService.updateDocument(request, userId);
        return ResultUtils.ok(document);
    }

    /**
     * 删除文档
     *
     * @param id 文档ID
     * @param request Http请求
     * @return 删除结果
     */
    @PostMapping("/delete/{id}")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<Boolean> deleteDocument(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        boolean deleted = collabDocumentService.deleteDocument(id, userId);
        return ResultUtils.ok(deleted);
    }

    /**
     * 获取文档详情
     *
     * @param id 文档ID
     * @param request Http请求
     * @return 文档详情
     */
    @GetMapping("/get/{id}")
//    //@AuthCheck(mustRole = "user")
    public ResultUtils<CollabDocumentVO> getDocumentById(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        CollabDocumentVO document = collabDocumentService.getDocumentById(id, userId);
        return ResultUtils.ok(document);
    }

    /**
     * 查询文档列表
     *
     * @param request 查询请求
     * @param request1 Http请求
     * @return 文档列表
     */
    @GetMapping("/list")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<Page<CollabDocumentVO>> listDocument(@ModelAttribute CollabDocumentQueryRequest request, HttpServletRequest request1) {
        Long userId = getCurrentUserId(request1);
        Page<CollabDocumentVO> page = collabDocumentService.queryDocumentPage(request, userId);
        return ResultUtils.ok(page);
    }

    /**
     * 获取文档版本列表
     *
     * @param docId 文档ID
     * @param request Http请求
     * @return 版本列表
     */
    @GetMapping("/versions/{docId}")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<List<CollabDocumentVersionVO>> listDocumentVersions(@PathVariable Long docId, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        List<CollabDocumentVersionVO> versions = collabDocumentService.getDocumentVersions(docId, userId);
        return ResultUtils.ok(versions);
    }

    /**
     * 获取文档指定版本
     *
     * @param docId 文档ID
     * @param version 版本�?
     * @param request Http请求
     * @return 版本详情
     */
    @GetMapping("/version/{docId}/{version}")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<CollabDocumentVersionVO> getDocumentVersion(@PathVariable Long docId, @PathVariable Integer version, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        CollabDocumentVersionVO versionVO = collabDocumentService.getDocumentVersion(docId, version, userId);
        return ResultUtils.ok(versionVO);
    }

    /**
     * 锁定文档
     *
     * @param id 文档ID
     * @param request Http请求
     * @return 锁定结果
     */
    @PostMapping("/lock/{id}")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<Boolean> lockDocument(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        boolean locked = collabDocumentService.lockDocument(id, 1, userId);
        return ResultUtils.ok(locked);
    }

    /**
     * 解锁文档
     *
     * @param id 文档ID
     * @param request Http请求
     * @return 解锁结果
     */
    @PostMapping("/unlock/{id}")
    //@AuthCheck(mustRole = "user")
    public ResultUtils<Boolean> unlockDocument(@PathVariable Long id, HttpServletRequest request) {
        Long userId = getCurrentUserId(request);
        boolean unlocked = collabDocumentService.lockDocument(id, 0, userId);
        return ResultUtils.ok(unlocked);
    }

    /**
     * todo 获取当前用户ID(公共方法获取)
     *
     * @param request Http请求
     * @return 用户ID
     */
    private Long getCurrentUserId(HttpServletRequest request) {
        // 从请求中获取用户ID，需要根据项目实际情况实�?
        // 这里假设从session或token中获�?
        Object userIdObj = request.getSession().getAttribute("userId");
        if (userIdObj == null) {
            throw new BusinessException(ResultCodeEnum.LOGIN_AUTH);
        }
        return Long.parseLong(userIdObj.toString());
    }
}

